#include "GetWeather.h"

using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;

GetWeather::GetWeather(void):__pButtonGetName(null),__pButtonGetPos(null),__pEditFieldName(null)
		,__pEditFieldLat(null),__pEditFieldLon(null),CityName(""),Lat(""),Lon(""),__pWeatherPopup(null)
{
}

GetWeather::~GetWeather(void)
{
}

bool
GetWeather::Initialize()
{
	// Construct an XML form
	Construct(L"IDF_GETWEATHER");

	return true;
}

result
GetWeather::OnInitializing(void)
{
	result r = E_SUCCESS;

	// TODO: Add your initialization code here

	// Get a button via resource ID
	__pButtonGetName = static_cast<Button *>(GetControl(L"IDC_BUTTON_GET_NAME"));
	if (__pButtonGetName != null)
	{
		__pButtonGetName->SetActionId(ID_BUTTON_GET_NAME);
		__pButtonGetName->AddActionEventListener(*this);
	}
	__pButtonGetPos = static_cast<Button *>(GetControl(L"IDC_BUTTON_GET_POS"));
	if (__pButtonGetPos != null)
		{
		__pButtonGetPos->SetActionId(ID_BUTTON_GET_POS);
		__pButtonGetPos->AddActionEventListener(*this);
		}
	__pEditFieldName = static_cast<EditField *>(GetControl(L"IDC_EDITFIELD1"));
	if (__pEditFieldName != null)
		{
		__pEditFieldName->AddTextEventListener(*this);
		}
	__pEditFieldLat = static_cast<EditField *>(GetControl(L"IDC_EDITFIELD2"));
	if (__pEditFieldLat != null)
		{
		__pEditFieldLat->AddTextEventListener(*this);
		}
	__pEditFieldLon = static_cast<EditField *>(GetControl(L"IDC_EDITFIELD3"));
	if (__pEditFieldLon != null)
		{
		__pEditFieldLon->AddTextEventListener(*this);
		}
	__pWeatherPopup = new WeatherPopup();
	__pWeatherPopup->Construct();

	return r;
}

result
GetWeather::OnTerminating(void)
{
	result r = E_SUCCESS;

	// TODO: Add your termination code here

	return r;
}

void
GetWeather::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
{
	switch(actionId)
	{
	case ID_BUTTON_GET_NAME:
		{
			AppLog("Get_name Button is clicked! \n");
			CityName=__pEditFieldName->GetText();
			__pWeatherPopup->getCityCode(CityName);
		}
		break;
	case ID_BUTTON_GET_POS:
	{
		AppLog("Get_pos button is clicked!\n");
		Lat=__pEditFieldLat->GetText();
		Lon=__pEditFieldLon->GetText();
		__pWeatherPopup->getWeatherByLatLon(Lat,Lon);
	}
	break;
	default:
		break;
	}
}

void
GetWeather::OnTextValueChanged(const Osp::Ui::Control& source)
{
    // Todo:
}

void
GetWeather::OnTextValueChangeCanceled(const Osp::Ui::Control& source)
{
    // Todo:
}

